package com.chain.qs.modules.biz.web;


import com.chain.qs.common.persistence.Page;
import com.chain.qs.common.utils.DateUtils;
import com.chain.qs.common.utils.StringUtils;
import com.chain.qs.common.utils.excel.ExportExcel;
import com.chain.qs.modules.biz.entity.*;
import com.chain.qs.modules.biz.service.*;
import com.chain.qs.modules.sys.entity.Office;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 设备类型Controller
 *
 * @author Wang zhiping
 */
@Controller
@RequestMapping(value = "${adminPath}/biz/ehRecord")
public class EquipmentItemRecordController extends SeniorController {
    @Resource
    private StationGroupCfgExtendService sgcfgService;
    @Resource
    private StationService stationService;
    @Resource
    private EquipmentService equipmentService;
    @Resource
    private EquipmentItemRecordSensorService sensorService;
    @Resource
    private EquipmentItemRecordLYEnvironmentDevService LYEnvironmentDevService;
    @Resource
    private EquipmentItemRecordLYUPSDevService LYUPSDevService;
    @Resource
    private EquipmentItemRecordCAHCTransDevService CAHCTransDevService;
    @Resource
    private EquipmentItemRecordTransmitterPanda1kWService Panda1kWTransDevService;
    @Resource
    private EquipmentItemRecordCAGospell1kWSpDevService CAGospell1kWDevService;
    @Resource
    private EquipmentGroupItemService itemService;

    @ModelAttribute("equipmentItemRecordSuper")
    public EquipmentItemRecordSuper getRecordDevSuper(String sgroupId, String gitemId, String timeBegin, String timeEnd, String etypeKid, String recordColumn) {
        return new EquipmentItemRecordSuper(sgroupId, gitemId, timeBegin, timeEnd, etypeKid, recordColumn);
    }

    @RequestMapping(value = "")
    public String sgroup(Model model) {
        model.addAttribute("list", stationService.findListWithOffice(new Office()));
        return "modules/biz/equItemHisList";
    }

    @RequestMapping(value = "graph")
    public String graphIndex(Model model) {
        model.addAttribute("list", stationService.findListWithOffice(new Office()));
        return "modules/biz/equItemHisGraph";
    }


    /**
     * 历史记录历史查询
     * @param equipmentItemRecordSuper
     * @param request
     * @param response
     * @return
     */

    @RequestMapping(value = "QHHumTemDevRecords")
    public String sensorRecords(EquipmentItemRecordSuper equipmentItemRecordSuper, HttpServletRequest request, HttpServletResponse response) {//查询提交功能
        List<String> titleList = new ExportExcel().titleList(EquipmentItemRecordSensor.class, 1);
        Page<EquipmentItemRecordSensor> page = sensorService.findPage(new Page<EquipmentItemRecordSensor>(request, response), new EquipmentItemRecordSensor(equipmentItemRecordSuper.getSgroupId(),equipmentItemRecordSuper.getGitemId(),equipmentItemRecordSuper.getTimeBegin(),equipmentItemRecordSuper.getTimeEnd(),equipmentItemRecordSuper.getRecordColumn()));
        page.setTitleList(titleList);
        new ExportExcel(EquipmentItemRecordSensor.class).setDataList(page.getList(), 1);
        page.setDataList(ExportExcel.dataList);
        return renderString(response, page);
    }

    @RequestMapping(value = "CAEnvironmentDevRecords")
    public String LYEnvironmentDevRecords(EquipmentItemRecordSuper equipmentItemRecordSuper, HttpServletRequest request, HttpServletResponse response) {//查询提交功能
        List<String> titleList = new ExportExcel().titleList(EquipmentItemRecordLYEnvironmentDev.class, 1);
        Page<EquipmentItemRecordLYEnvironmentDev> page = LYEnvironmentDevService.findPage(new Page<EquipmentItemRecordLYEnvironmentDev>(request, response), new EquipmentItemRecordLYEnvironmentDev(equipmentItemRecordSuper.getSgroupId(),equipmentItemRecordSuper.getGitemId(),equipmentItemRecordSuper.getTimeBegin(),equipmentItemRecordSuper.getTimeEnd(),equipmentItemRecordSuper.getRecordColumn()));
        page.setTitleList(titleList);
        new ExportExcel(EquipmentItemRecordLYEnvironmentDev.class).setDataList(page.getList(), 1);
        page.setDataList(ExportExcel.dataList);
        return renderString(response, page);
    }

    @RequestMapping(value = "CAUPSDevRecords")
    public String LYUPSDevRecords(EquipmentItemRecordSuper equipmentItemRecordSuper, HttpServletRequest request, HttpServletResponse response) {//查询提交功能
        List<String> titleList = new ExportExcel().titleList(EquipmentItemRecordLYUPSDev.class, 1);
        Page<EquipmentItemRecordLYUPSDev> page = LYUPSDevService.findPage(new Page<EquipmentItemRecordLYUPSDev>(request, response), new EquipmentItemRecordLYUPSDev(equipmentItemRecordSuper.getSgroupId(),equipmentItemRecordSuper.getGitemId(),equipmentItemRecordSuper.getTimeBegin(),equipmentItemRecordSuper.getTimeEnd(),equipmentItemRecordSuper.getRecordColumn()));
        page.setTitleList(titleList);
        new ExportExcel(EquipmentItemRecordLYUPSDev.class).setDataList(page.getList(), 1);
        page.setDataList(ExportExcel.dataList);
        return renderString(response, page);
    }
    @RequestMapping(value = "CAHCTransDevRecords")
    public String CAHCTransDevRecords(EquipmentItemRecordSuper equipmentItemRecordSuper, HttpServletRequest request, HttpServletResponse response) {//查询提交功能
        List<String> titleList = new ExportExcel().titleList(EquipmentItemRecordCAHCTransDev.class, 1);
        Page<EquipmentItemRecordCAHCTransDev> page = CAHCTransDevService.findPage(new Page<EquipmentItemRecordCAHCTransDev>(request, response), new EquipmentItemRecordCAHCTransDev(equipmentItemRecordSuper.getSgroupId(),equipmentItemRecordSuper.getGitemId(),equipmentItemRecordSuper.getTimeBegin(),equipmentItemRecordSuper.getTimeEnd(),equipmentItemRecordSuper.getRecordColumn()));
        page.setTitleList(titleList);
        new ExportExcel(EquipmentItemRecordCAHCTransDev.class).setDataList(page.getList(), 1);
        page.setDataList(ExportExcel.dataList);
        return renderString(response, page);
    }
    @RequestMapping(value = "Panda1kWTransDevRecords")
    public String Panda1kWTransDevRecords(EquipmentItemRecordSuper equipmentItemRecordSuper, HttpServletRequest request, HttpServletResponse response) {//查询提交功能
        List<String> titleList = new ExportExcel().titleList(EquipmentItemRecordTransmitterPanda1kW.class, 1);
        Page<EquipmentItemRecordTransmitterPanda1kW> page = Panda1kWTransDevService.findPage(new Page<EquipmentItemRecordTransmitterPanda1kW>(request, response), new EquipmentItemRecordTransmitterPanda1kW(equipmentItemRecordSuper.getSgroupId(),equipmentItemRecordSuper.getGitemId(),equipmentItemRecordSuper.getTimeBegin(),equipmentItemRecordSuper.getTimeEnd(),equipmentItemRecordSuper.getRecordColumn()));
        page.setTitleList(titleList);
        new ExportExcel(EquipmentItemRecordTransmitterPanda1kW.class).setDataList(page.getList(), 1);
        page.setDataList(ExportExcel.dataList);
        return renderString(response, page);
    }
    @RequestMapping(value = "CAGospell1kWSpDevRecords")
    public String CAGospell1kWSpDevRecords(EquipmentItemRecordSuper equipmentItemRecordSuper, HttpServletRequest request, HttpServletResponse response) {//查询提交功能
        List<String> titleList = new ExportExcel().titleList(EquipmentItemRecordCAGospell1kWSpDev.class, 1);
        Page<EquipmentItemRecordCAGospell1kWSpDev> page = CAGospell1kWDevService.findPage(new Page<EquipmentItemRecordCAGospell1kWSpDev>(request, response), new EquipmentItemRecordCAGospell1kWSpDev(equipmentItemRecordSuper.getSgroupId(),equipmentItemRecordSuper.getGitemId(),equipmentItemRecordSuper.getTimeBegin(),equipmentItemRecordSuper.getTimeEnd(),equipmentItemRecordSuper.getRecordColumn()));
        page.setTitleList(titleList);
        new ExportExcel(EquipmentItemRecordCAGospell1kWSpDev.class).setDataList(page.getList(), 1);
        page.setDataList(ExportExcel.dataList);
        return renderString(response, page);
    }
    /**
     * 历史记录图标查询
     * @param response
     * @param equipmentItemRecordSuper
     * @return
     */


    @RequestMapping(value = "QHHumTemDevDevGraph")
    public String sensorGraph(HttpServletResponse response, EquipmentItemRecordSuper equipmentItemRecordSuper) {//查询提交功能
        return renderString(response, sensorService.conditionList(equipmentItemRecordSuper));
    }

    @RequestMapping(value = "CAEnvironmentDevGraph")
    public String LYEnvironmentDevGraph(HttpServletResponse response, EquipmentItemRecordSuper equipmentItemRecordSuper) {//查询提交功能
        return renderString(response, LYEnvironmentDevService.conditionList(equipmentItemRecordSuper));
    }

    @RequestMapping(value = "CAUPSDevGraph")
    public String LYUPSDevGraph(HttpServletResponse response, EquipmentItemRecordSuper equipmentItemRecordSuper) {//查询提交功能
        return renderString(response, LYUPSDevService.conditionList(equipmentItemRecordSuper));
    }

    /**
     * 其它
     * @param response
     * @param itemId
     * @return
     */

    @RequestMapping(value = "etypeKid")
    public String etypeKid(HttpServletResponse response, String itemId) {//查询提交功能
        return renderString(response, equipmentService.etypeKid(itemId));
    }

    @RequestMapping(value = "form")
    public String form(@RequestParam(required = false, value = "sgroupId") String sgroupId, @RequestParam(required = false, value = "groupId") String groupId, @RequestParam(required = false, value = "gitemId") String gitemId, Model model) {
        StationGroupCfgExtend cfg2 = sgcfgService.getOne(sgroupId, groupId, gitemId);
        model.addAttribute("cfg2", cfg2);
        model.addAttribute("sgroupId", sgroupId);
        return "modules/biz/stationGroupCfgForm";
    }

    @RequestMapping(value = "exportRecords")
    public void exportRecords(String officeId, EquipmentItemRecordSuper equipmentItemRecordSuper, OfficeBiz officebiz, HttpServletResponse response, RedirectAttributes redirectAttributes) {
        try { // 生成文件的名称
            StringBuilder fileName = new StringBuilder("");
            // 站名
            String stationName = officebiz.getName();
            if (StringUtils.isNotBlank(officeId)) {
                stationName = officeService.get(officeId).getName();
            }
            // 站组名
            String sGroupName = sgroupService.get(equipmentItemRecordSuper.getSgroupId()).getName();
            // 设备名称
            String itemName = itemService.get(equipmentItemRecordSuper.getGitemId()).getName();

            if (stationName != null) {
                fileName.append(stationName);
            }
            if (sGroupName != null) {
                fileName.append(sGroupName);
            }
            if (itemName != null) {
                fileName.append(itemName);
            }
            String fileTitle = fileName.toString();
            fileName.append(DateUtils.getDate("yyyyMMddHHmmss")).append(".xlsx");

            /**
             * 需要根据需求配置
             */

            switch (equipmentItemRecordSuper.getTypeId()) {
                case "LYEnvironmentDev":
                    new ExportExcel(fileTitle, EquipmentItemRecordLYEnvironmentDev.class).setDataList(LYEnvironmentDevService.conditionList(equipmentItemRecordSuper)).write(response, fileName.toString()).dispose();
                    break;
                case "HumTemDev":
                    new ExportExcel(fileTitle, EquipmentItemRecordSensor.class).setDataList(sensorService.conditionList(equipmentItemRecordSuper)).write(response, fileName.toString()).dispose();
                    break;
                case "LYUPSDev":
                    new ExportExcel(fileTitle, EquipmentItemRecordLYUPSDev.class).setDataList(LYUPSDevService.conditionList(equipmentItemRecordSuper)).write(response, fileName.toString()).dispose();
                    break;
            }
        } catch (Exception e) {
            addMessage(redirectAttributes, "导出失败！失败信息：" + e.getMessage());
        }
    }
}
